<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-11-15
 * Time: 下午 3:15
 */
class cartControl extends MobileControl{

    public function __construct() {
        if(!$_SESSION['member_id']){
            Tpl::showpage('login');
        }
    }

    /**
     * 加入购物车，登录后存入购物车表
     * 存入COOKIE，由于COOKIE长度限制，最多保存5个商品
     * 未登录不能将优惠套装商品加入购物车，登录前保存的信息以goods_id为下标
     */
    public function addOp(){
        $model_goods = Model('goods');
        if (is_numeric($_GET['goods_id'])) {
            //商品加入购物车(默认)
            $goods_id = intval($_GET['goods_id']);
            $quantity = intval($_GET['quantity']);
            $goods_info = $model_goods->getGoodsOnlineInfoAndPromotionById($goods_id);
        }

        //已登录状态，存入数据库,未登录时，跳转到登录页面
        if($_SESSION['member_id']) {
            $goods_info['buyer_id'] = $_SESSION['member_id'];

            $model_cart = Model('cart');
            $insert = $model_cart->addCart($goods_info,'db',$quantity);
            if ($insert) {
                $data = array('state'=>'true', 'num' => $model_cart->cart_goods_num, 'amount' => ncPriceFormat($model_cart->cart_all_price));
            } else {
                $data = array('state'=>'false');
            }
        }else {
            $data = array('state'=>'false');//未登录
        }
        exit(json_encode($data));
    }

    /**
     * ajax 更改购物车数量
     */
    public function updateOp(){
        $data = array('state'=>'false');
        if($_SESSION['member_id']) {
            $cart_id = intval($_GET['cart_id']);
            $quantity = intval($_GET['quantity']);

            $model_cart = Model('cart');
            $model_cart->editCart(array('goods_num'=>$quantity),array('cart_id'=>$cart_id,'buyer_id'=>$_SESSION['member_id']));
            $data = array('state'=>'true');
        }
        exit(json_encode($data));
    }

    /**
     * 购物车列表
     */
    public function goods_cartOp() {
        $type = $_GET['type'];
        $model_cart = Model('cart');
        //购物车列表
        $cart_list  = $model_cart->listCart('db',array('buyer_id'=>$_SESSION['member_id']));
        foreach ($cart_list as $key => $cart_info) {
            if(!empty($cart_info['goods_id'])) {
                $goods_detail = Model('goods')->getGoodsInfoByID($cart_info['goods_id'],'year_rate');
                $cart_info['year_rate'] = $goods_detail['year_rate'];
                $cart_info['goods_total_price'] = $cart_info['goods_price']*$cart_info['goods_num'];
                $cart_info['goods_storage'] = $goods_detail['goods_storage'];
            }else{
                $cart_info['year_rate'] = 6;
            }
            $cart_list[$key] = $cart_info;
        }

        Tpl::output('cart_list',$cart_list);

        //标识 购买流程执行第几步
        Tpl::output('buy_step','step1');
        if($type=='update'){
            Tpl::showpage('goods_cart-update');
        }else{
            Tpl::output('cart_goods_num',$model_cart->cart_goods_num);
            Tpl::output('cart_all_price',$model_cart->cart_all_price);
            Tpl::showpage(empty($cart_list) ? 'cart_empty' : 'goods_cart');
        }
    }

    /**
     * 删除购物车
     */
    public function delOp(){
        $cart_ids = intval($_GET['cart_ids']);
        if($cart_ids < 0) return ;
        $model_cart = Model('cart');
        $data = array();
        $cart_id_array = explode('|', $_GET['cart_ids']);
        foreach ($cart_id_array as $cart_id) {
            if ($_SESSION['member_id']) {
                //登录状态下删除数据库内容
                $delete = $model_cart->delCart('db', array('cart_id' => $cart_id, 'buyer_id' => $_SESSION['member_id']));
                if ($delete) {
                    $data['state'] = 'true';
                    $data['quantity'] = $model_cart->cart_goods_num;
                    $data['amount'] = $model_cart->cart_all_price;
                } else {
                    $data['msg'] = Language::get('cart_drop_del_fail', 'UTF-8');
                }
            }
        }
        setNcCookie('cart_goods_num',$model_cart->cart_goods_num,2*3600);
        $json_data = json_encode($data);
        if (isset($_GET['callback'])) {
            $json_data = $_GET['callback']=='?' ? '('.$json_data.')' : $_GET['callback']."($json_data);";
        }
        exit($json_data);
    }

    /**
     * 检查商品是否符合加入购物车条件
     * @param unknown $goods
     * @param number $quantity
     */
    private function _check_goods($goods_info, $quantity) {
        if(empty($quantity)) {
            exit(json_encode(array('msg'=>Language::get('wrong_argument','UTF-8'))));
        }
        if(empty($goods_info)) {
            exit(json_encode(array('msg'=>Language::get('cart_add_goods_not_exists','UTF-8'))));
        }
        if ($goods_info['store_id'] == $_SESSION['store_id']) {
            exit(json_encode(array('msg'=>Language::get('cart_add_cannot_buy','UTF-8'))));
        }
        if(intval($goods_info['goods_storage']) < 1) {
            exit(json_encode(array('msg'=>Language::get('cart_add_stock_shortage','UTF-8'))));
        }
        if(intval($goods_info['goods_storage']) < $quantity) {
            exit(json_encode(array('msg'=>Language::get('cart_add_too_much','UTF-8'))));
        }
        if ($goods_info['is_virtual'] || $goods_info['is_fcode'] || $goods_info['is_presell']) {
            exit(json_encode(array('msg'=>'该商品不允许加入购物车，请直接购买','UTF-8')));
        }
    }
}